//
//  ScenicViewController.m
//  小胖兔旅行指南
//
//  Created by 小胖兔 on 15/4/10.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "ScenicViewController.h"
#import "EncapsulationMethod.h"
#import "ScenicMainTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ScenicSecondViewController.h"
#import "UIScrollView+PullLoad.h"


@interface ScenicViewController ()<UITableViewDataSource, UITableViewDelegate, PullDelegate>

@property (nonatomic, retain) UITableView *tableScenic;

@property (nonatomic, retain) NSMutableArray *tableArray;

@end

@implementation ScenicViewController

- (void)dealloc
{
    [_tableArray release];
    [_tableScenic release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"最美景点";
    
    [self createSecenic];

}

#pragma mark - tableView创建
- (void)createSecenic
{
    self.tableScenic = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain] autorelease];
//    [_tableScenic setContentInset:UIEdgeInsetsMake(-36, 0, 0, 0)];//
    
    _tableScenic.delegate = self;
    _tableScenic.dataSource = self;
    
    [self.tableScenic setPullDelegate:self];
    [self.tableScenic setCanPullDown:YES];
    [_tableScenic registerClass:[ScenicMainTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableScenic];
//    _tableScenic.bounces = NO;
    
    // http://tubu.ibuzhai.com/rest/v1/trails?app_version=1.9.0&city=0&crowd=0&device_type=1&page=1&page_size=0&trait=0
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic setObject:@"1.9.0" forKey:@"app_version"];
    [dic setObject:@"0" forKey:@"city"];
    [dic setObject:@"0" forKey:@"crowd"];
    [dic setObject:@"1" forKey:@"device_type"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"0" forKey:@"page_size"];
    [dic setObject:@"0" forKey:@"trait"];

    
    
    [EncapsulationMethod connectWithUrl:@"http://tubu.ibuzhai.com/rest/v1/trails" parmaters:dic finishBlock:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@", dictionary);
        self.tableArray = [dictionary objectForKey:@"trails"];
        [_tableScenic reloadData];
    }];

    
}


#pragma mark -
#pragma 下拉刷新
- (void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        [self performSelector:@selector(downLoad) withObject:nil afterDelay:0];
    }
}

- (void)downLoad
{
    [self.tableScenic reloadData];
    [self.tableScenic stopLoadWithState:PullDownLoadState];
}


#pragma mark - cell创建

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ScenicMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    NSDictionary *inDic = [_tableArray objectAtIndex:indexPath.row];
    
    
    NSURL *urlImage = [NSURL URLWithString:[inDic objectForKey:@"cover"]];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"dengdaizhong" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    UIImage *imageWait = [UIImage imageWithData:data];
    [cell.photoBackground sd_setImageWithURL:urlImage placeholderImage: imageWait];//等待时图片
    cell.sightName.text = [inDic objectForKey:@"name"];
    cell.sightName.textAlignment = NSTextAlignmentCenter;//label文字居中
    cell.sightName.font = [UIFont systemFontOfSize:24];//字号
    cell.sightName.textColor = [UIColor whiteColor];
    cell.sightAddress.text = [inDic objectForKey:@"destination"];
    cell.sightAddress.textAlignment = NSTextAlignmentCenter;
    cell.sightAddress.font = [UIFont systemFontOfSize:15];
    cell.sightAddress.textColor = [UIColor whiteColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    
    ScenicSecondViewController *second = [[ScenicSecondViewController alloc] init];
    second.myId = [dic objectForKey:@"id"];
    [second setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:second animated:YES];
    
    return [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
